package org.l.codemaker;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * Created by 严秋旺 on 2016/6/22.
 */
public class MainFrame {
    private JTextField packageTextField;
    private JTextField collectionTextField;
    private JTextField objectTextField;
    private JPanel panel;
    private JButton buttonMake;
    private JTextArea infoTextArea;
    private Maker maker;

    public MainFrame() {
        buttonMake.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MakeParam makeParam = new MakeParam();
                makeParam.setPackageName(packageTextField.getText());
                makeParam.setCollectionName(collectionTextField.getText());
                makeParam.setObjectName(objectTextField.getText());
                try {
                    maker.makeFile(makeParam);
                } catch (Exception e1) {
                    e1.printStackTrace();
                    Logger.show("代码生成异常", e1);
                }

            }
        });
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame("MainFrame");
        MainFrame mainFrame = new MainFrame();
        frame.setContentPane(mainFrame.panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        //frame.setResizable(false);
        frame.pack();

        if (args.length > 0) {
            mainFrame.packageTextField.setText(args[0]);
        }

        Logger.textArea = mainFrame.infoTextArea;

        try {
            mainFrame.maker = new Maker();
        } catch (IOException e) {
            e.printStackTrace();
            Logger.show("模板引擎初始化异常", e);
        }

        frame.setVisible(true);
    }
}
